package com.flash.dataU.test.controller;

import com.flash.dataU.test.entity.Project;
import com.flash.dataU.test.entity.Server;
import com.flash.dataU.test.entity.ServerType;
import com.flash.dataU.test.service.impl.ProjectService;
import com.flash.dataU.test.service.impl.ServerService;
import com.flash.dataU.test.service.impl.ServerTypeService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * .
 *
 * @author sunym1993
 * @version 0.0.1-SNAPSHOT
 * @since 2017年12月14日 16时07分
 */
@Controller
@RequestMapping("rel")
public class RelController {

    @Autowired
    private ServerService serverService;

    @Autowired
    private ServerTypeService serverTypeService;

    @Autowired
    private ProjectService projectService;

    @RequestMapping("index")
    public String index(HttpServletRequest request){

        List<Server> servers = serverService.findAllList();
        List<Project> projects = projectService.findAllList();
        setType(servers);

        request.setAttribute("servers", servers);
        request.setAttribute("projects", projects);

        return "rel";
    }

    @RequestMapping("addProjectForServer")
    public String addProjectForServer(int projectId, int serverId, HttpServletRequest request){
        serverService.addProject(serverId, projectId);
        return index(request);
    }

    /**
     * 填充server中的类型、项目和负载
     * @param servers
     */
    private void setType(List<Server> servers) {
        for (Server server : servers) {
            int typeId = server.getType().getId();
            ServerType serverType = serverTypeService.get(typeId);
            server.setType(serverType);

        }
    }

}
